#include "ShowListManager.h"

ShowListManager::ShowListManager(void)
{
  _used = 0;
}

ShowListManager::~ShowListManager(void)
{
  glDeleteLists (1, _used);
}

GLuint ShowListManager::createNewList (GLenum mode)
{
  // Find a free list name in _freeList
  while (_freeList.size ())
  {
    GLuint listName = *(_freeList.begin ());
    _freeList.erase (_freeList.begin ());
    if (!glIsList (listName))
    {
      glNewList (listName, mode);
      return listName;
    }
  }
  
  while (_used != -1) // That is the maximum of GLuint
  {
    ++_used;
    GLuint listName = _used;
    if (!glIsList (listName))
    {
      glNewList (listName, mode);
      return listName;
    }
  }

  throw (string ("Show list is full"));
}

void ShowListManager::deleteList (GLuint listName)
{
  if (!glIsList (listName))
    return;
  
  glDeleteLists (listName, 1);
  _freeList.push_back (listName);
}